<?php
App::uses('AppController', 'Controller');
/**
 * Applications Controller
 *
 * @property Application $Application
 */
class ApplicationsController extends AppController {


/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Application->recursive = 0;
		$this->set('applications', $this->paginate());
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Application->id = $id;
		if (!$this->Application->exists()) {
			throw new NotFoundException(__('Invalid application'));
		}
		$this->set('application', $this->Application->read(null, $id));
	}
/**
 * remove applications
 *
 * @return void
 */
 public function delete($id = null) {
 		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Application->id = $id;
		if (!$this->Application->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->Application->delete()) {
			$this->Session->setFlash(__('Application deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Application was not deleted'));
		$this->redirect(array('action' => 'index'));
}
/**
 * add applications
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Application->create();
			$this->request->data['Application']['user_id'] = $this -> Auth -> user('id');
			$this->request->data['Application']['status'] = 0;
			$this->request->data['Application']['final_status'] = 2;
			$this->request->data['Application']['template_pointer'] = 0;

			// save
			if ($this->Application->save($this->request->data)) {
				$layerNo = 1;
				$userNo = 1;
				while (true) {
					if (isset($this->request->data['Layer' . $layerNo]['Tar'. $userNo])) {
						while(true) {
							if (isset($this->request->data['Layer' . $layerNo]['Tar' . $userNo])) {
								$this->Application->Flownode->create();
								$flownodeData = array(
									'Flownode' => array(
										'application_id' => $this->Application->id,
										'user_id' => $this->request->data['Layer' . $layerNo]['Tar' . $userNo],
										'loc_index' => $layerNo - 1,
										'loc_identity' => $this->request->data['Layer' . $layerNo]['Identity' . $userNo],
										'status' => 0
									)
								);
								if (!$this->Application->Flownode->save($flownodeData)) {
									$this->Session->setFlash(__('The application could not be saved. Please, try again.'));
									break;
								} else {
									$userNo++;
								}
							} else {
								$userNo = 1;
								$layerNo ++;
								break;
							}
						}
					} else {
						break;
					}
				}
				$this->Session->setFlash(__('The application has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The application could not be saved. Please, try again.'));
			}
		}
		$users = $this->Application->User->find('list');
		$this->set(compact('users'));
	}


/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Application->id = $id;
		if (!$this->Application->exists()) {
			throw new NotFoundException(__('Invalid application'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Application->save($this->request->data)) {
				$this->Session->setFlash(__('The application has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The application could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Application->read(null, $id);
		}
		$users = $this->Application->User->find('list');
		$this->set(compact('users'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
    public function isAuthorized($user) {
        if ($this->action == 'add') {
			return true;
		} 
		if (in_array($this->action, array('edit', 'delete'))) {
			$applicationId = $this->request->params['pass'][0];
			if ($this->Application->isOwnedBy($applicationId, $user['id'])) {
				return true;
			}
		}
        return parent::isAuthorized($user);
    }

}
